package com.sl.plat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.water.entity.BRiskPerson;
import com.sl.water.service.BRiskPersonService;
import com.sl.water.vo.BRiskPersonIVO;
import com.sl.water.vo.BaseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plat/risk/warn/person")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预警-预警人员", description = "提供预警人员增、删、改、查、列表查询相关功能")
public class BRiskPersonController {

    private final BRiskPersonService bRiskPersonService;

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<BRiskPerson>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size,
            @Parameter(
                    description = "类型（网站，短信，APP，小程序）",
                    example = "小程序",
                    required = false
            )
            @RequestParam(required = false) String kind
    ) {
        Page<BRiskPerson> bRiskPersonPage = bRiskPersonService.pageList(page, size, kind);
        return ResponseData.success(bRiskPersonPage);
    }

    @GetMapping
    @Operation(summary = "列表")
    public ResponseData<List<BRiskPerson>> getList() {
        return ResponseData.success(bRiskPersonService.list());
    }

    @PostMapping
    @Operation(summary = "新增")
    public ResponseData save(@RequestBody BRiskPersonIVO bRiskPersonVO) {
        BRiskPerson bRiskPerson = BaseVO.convert(bRiskPersonVO, BRiskPerson.class);
        bRiskPersonService.save(bRiskPerson);
        return ResponseData.success(bRiskPerson.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除")
    public ResponseData removeById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        bRiskPersonService.removeById(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改")
    public ResponseData updateById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id,
                                   @RequestBody BRiskPersonIVO bRiskPersonVO) {
        BRiskPerson bRiskPerson = BaseVO.convert(bRiskPersonVO, BRiskPerson.class);
        bRiskPerson.setId(id);
        bRiskPersonService.updateById(bRiskPerson);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询")
    public ResponseData<BRiskPerson> getById(
            @Parameter(
                    description = "主键",
                    required = true,
                    example = "1"
            )
            @PathVariable long id) {
        return ResponseData.success(bRiskPersonService.getById(id));
    }

}